import instance from './request';

// 定义基础URL，与后端接口地址对应
const baseURL = 'http://172.20.10.10:3000';

export interface addFormInlineTS {
  username: string,
  phone: string,
  id: string,
  department: string,
  employeestype: string
}
const token =localStorage.getItem('token')

// 获取所有用户
export const getAllUsers = () => {
  return instance.get(`http://172.20.10.10:3000/user`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按姓名查询用户
export const queryUserByUsername = (username:string) => {
  return instance.get(`${baseURL}/user?username=${username}`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按手机号查询用户
export const queryUserByPhone = (phone:string) => {
  return instance.get(`${baseURL}/user?phone=${phone}`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按部门查询用户
export const queryUserByDepartment = (department:string) => {
  return instance.get(`${baseURL}/user?department=${department}`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};
//删除接口
export const deleteUser = (id:string) => {
  return instance.delete(`${baseURL}/user/${id}`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};


// 按多条件组合查询（示例：同时传姓名和部门）
export const queryUserByMultiConditions = ({ username, phone, department, status }:{username:string,phone:string,department:string,status:string}) => {
  const obj={
    username,
    phone,
    department
  }
    const params = new URLSearchParams(obj).toString();
  return instance.get(`${baseURL}/user?${params}`,{
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 添加用户
export const addTable = (userData: addFormInlineTS) => {
  return instance.post(`${baseURL}/user`, userData, {
    headers: {
      'Content-Type': 'application/json', // 明确请求体为JSON格式
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    }
  });
};




